import axios, { 
  AxiosInstance, 
  InternalAxiosRequestConfig,
  AxiosResponse, 
  AxiosError 
} from 'axios';
import { API_URLS } from '../utils/api';
import { envConfig } from '../utils/get-env';

// 创建 axios 实例
const apiInstance: AxiosInstance = axios.create({
  baseURL: envConfig.apiBaseUrl,  // 使用envConfig中的API地址
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json',
  },
  withCredentials: true  // 允许跨域请求携带凭证
});

// 请求拦截器
apiInstance.interceptors.request.use(
  (config: InternalAxiosRequestConfig) => {
    console.log('Request Config:', {
      url: config.url,
      method: config.method,
      data: config.data,
      headers: config.headers,
      baseURL: config.baseURL  // 添加baseURL到日志
    });
    return config;
  },
  (error: AxiosError) => {
    console.error('Request Error:', error);
    return Promise.reject(error);
  }
);

// 响应拦截器
apiInstance.interceptors.response.use(
  (response: AxiosResponse) => {
    console.log('Response Data:', response.data);
    return response;
  },
  (error: AxiosError) => {
    console.error('Response Error:', {
      message: error.message,
      response: error.response?.data,
      status: error.response?.status,
    });
    return Promise.reject(error);
  }
);

// 发送验证码
export const sendVerificationCode = async (email: string) => {
  try {
    console.log('Sending verification code to:', email);
    const response = await apiInstance.post('/auth/send-verification-code', { email });
    console.log('Verification code sent successfully:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error sending verification code:', error);
    throw error;
  }
};

// 验证验证码
export const verifyCode = async (email: string, code: string) => {
  try {
    console.log('Verifying code for:', email);
    const response = await apiInstance.post('/auth/verify-code', { email, code });
    console.log('Code verification successful:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error verifying code:', error);
    throw error;
  }
};

// 用户注册
export const register = async (userData: any) => {
  try {
    console.log('Registering user:', userData.email);
    const response = await apiInstance.post(API_URLS.REGISTER, userData);
    console.log('Registration successful:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error registering user:', error);
    throw error;
  }
};

// 用户登录
export const login = async (loginData: { email: string; password?: string; code?: string }) => {
  try {
    console.log('Logging in user:', loginData.email);
    const response = await apiInstance.post(API_URLS.LOGIN, loginData);
    console.log('Login successful:', response.data);
    return response.data;
  } catch (error) {
    console.error('Error logging in:', error);
    throw error;
  }
};

export const getCurrentUser = async () => {
  try {
    const response = await apiInstance.get(API_URLS.CURRENT_USER);
    return response.data;
  } catch (error) {
    console.error('Error getting current user:', error);
    throw error;
  }
}; 